<template>
  <div class="login-container">
    <van-nav-bar class="page-nav-bar"
                 title="登录/注册"
                 left-arrow>
    </van-nav-bar>

    <img src="@/assets/11.jpg"
         alt=""
         class="logo">
    <!-- 表单 -->
    <van-form @submit="onSubmit"
              ref="form"
              :show-error="false">
      <van-field v-model="formInfo.mobile"
                 icon-prefix="toutiao"
                 left-icon="shouji"
                 name="mobile"
                 placeholder="手机号"
                 :rules="formRules.mobile"
                 clearable />
      <van-field v-model="formInfo.code"
                 icon-prefix="toutiao"
                 left-icon="yanzhengma"
                 name="code"
                 placeholder="验证码"
                 :rules="formRules.code"
                 clearable>
        <template #button>
          <van-button size="small"
                      type="primary"
                      native-type="button"
                      @click="sendSm()"
                      v-if="isCodeShow"
                      round
                      color="#7232dd"
                      plain>发送验证码</van-button>
          <van-button size="small"
                      color="#fff"
                      native-type="button"
                      v-else>
            <van-count-down :time="time"
                            format="ss s"
                            @finish="isCodeShow = true" />
          </van-button>

        </template>
      </van-field>
      <div style="margin: 16px;">
        <van-button block
                    type="info">立即登录</van-button>
        <van-notice-bar scrollable
                        color="pink"
                        background="#ecf9ff"
                        text="万能验证码:246810"
                        class="down" />

      </div>
    </van-form>
  </div>
</template>

<script>
import { login, getSendSm } from '@/api/user'
export default {
  name: 'index',
  data() {
    return {
      formInfo: {
        mobile: '18655629656',
        code: '246810'
      },
      formRules: {
        mobile: [
          { required: true, message: '请填写手机号' },
          { pattern: /^1[3|4|5|7|8|9][0-9]{9}$/, message: '手机号格式不正确' }
        ],
        code: [
          { required: true, message: '请填写验证码' },
          { required: /^d{6}$/, message: '验证码格式不正确' }
        ]
      },
      time: 1000 * 6,
      isCodeShow: true
    }
  },
  methods: {
    async onSubmit(values) {
      try {
        const { data: res } = await login(values)
        this.$toast.success('登录成功')
        console.log(res.data)
      } catch (error) {
        console.dir(error)
        this.$toast.fail('登录失败')
      }
    },
    async sendSm() {
      try {
        await this.$refs.form.validate('mobile')
      } catch (error) {
        return this.$toast.fail(error.message)
      }
      try {
        // 发送请求
        await getSendSm(this.formInfo.mobile)
        this.isCodeShow = false
        this.$toast.success('验证码发送成功')
      } catch (error) {
        this.$toast.fail('验证码发送失败')
      }
    }
  }
}
</script>

<style lang="less" scoped>
.login-container {
  .logo {
    width: 220px;
    display: block;
    margin: 40px auto;
    border-radius: 50%;
  }
  .tip {
    font-size: 30px;
    color: #999;
    text-align: center;
  }
  .down {
    margin-top: 50px;
  }
}
</style>